package com.vs.meepet.manager

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.vs.meepet.utils.LogUtils

import java.lang.ref.WeakReference
import java.util.LinkedList

enum class GlobalActivityManager {

    INSTANCE;

    companion object {
        const val TAG = "GlobalActivityManager"
    }

    private var mActivity: Activity? = null
    val actList = LinkedList<WeakReference<Activity?>>()
    var activityStack = mutableListOf<Activity?>()
    lateinit var application: Application

    fun init(application: Application) {
        this.application = application
        application.registerActivityLifecycleCallbacks(object :
            Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                currentActivity = activity
                addActivity(activity)
                activityStack.add(activity)
            }

            override fun onActivityStarted(activity: Activity) {

            }

            override fun onActivityResumed(activity: Activity) {
                currentActivity = activity
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
                removeActivity(activity)
                if (isCurrentActivity(activity)) {
                    currentActivity = null
                }
                activityStack.remove(activity)
            }
        })
    }


    private fun isCurrentActivity(activity: Activity): Boolean {
        return activity === currentActivity
    }

    var currentActivity: Activity?
        get() = mActivity
        private set(activity) {
            if (activity == null) {
                mActivity = null
                if (actList.size != 0) {
                    mActivity = actList[actList.size - 1].get()
                }
            } else if (mActivity == null || mActivity !== activity) {
                mActivity = activity
            }
        }

    private fun addActivity(activity: Activity?) {
        if (activity != null) {
            actList.add(WeakReference(activity))
        }
    }

    private fun removeActivity(activity: Activity?) {
        if (activity != null) {
            val iterator: MutableIterator<WeakReference<Activity?>> = actList.iterator()
            while (iterator.hasNext()) {
                val obj: WeakReference<Activity?> = iterator.next()
                if (obj.get() === activity) {
                    iterator.remove()
                    break
                }
            }
        }
    }

    fun hasActivity(clazz: Class<*>): Boolean {
        val iterator = actList.iterator()
        while (iterator.hasNext()) {
            val activity = iterator.next().get()
            if (activity != null && activity::class.java == clazz) {
                return true
            }
        }
        return false
    }

    fun finishActivity(clazz: Class<*>) {
        val iterator: MutableIterator<WeakReference<Activity?>> = actList.iterator()
        while (iterator.hasNext()) {
            val activity = iterator.next().get()
            if (activity != null && activity::class.java == clazz) {
                activity.finish()
                LogUtils.i(TAG, "finishActivity activity:$activity finish")
            }
        }
    }

    fun finishAllActivity(filter: FinishFilter? = null) {
        val iterator: MutableIterator<WeakReference<Activity?>> = actList.iterator()
        while (iterator.hasNext()) {
            val activity = iterator.next().get()
            if (activity != null) {
                if (filter == null) {
                    activity.finish()
                } else {
                    if (filter.apply(activity)) {
                        continue
                    } else {
                        activity.finish()
                    }
                }
                LogUtils.i(TAG, "finishAllActivity activity:$activity finish")
            }
        }
    }

    /**
     * 过滤，如果 activity 等于参数，则过滤
     */
    interface FinishFilter {
        fun apply(activity: Activity?): Boolean
    }
}